package com.project.gjzb.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.project.gjzb.dto.BusLineStationDto;
import com.project.gjzb.dto.ParamDto;
import com.project.gjzb.entity.BusLine;
import com.project.gjzb.entity.TobjBusLine;
import com.project.gjzb.mapper.BusLineMapper;
import com.project.gjzb.mapper.TobjBusLineMapper;
import com.project.gjzb.service.BusDirectionService;
import com.project.gjzb.service.BusLineService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class BusLineServiceImpl implements BusLineService {

    @Resource
    private BusLineMapper busLineMapper;
    @Resource
    private TobjBusLineMapper tobjBusLineMapper;
    @Resource
    BusDirectionService busDirectionService;

    @Override
    @DS("datacenter")
    public List<TobjBusLine> getList() {
        return tobjBusLineMapper.getList();
    }

    @Override
    @DS("datacenter")
    public TobjBusLine getById(BusLineStationDto busLineStationDto) {
        return tobjBusLineMapper.getById(busLineStationDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void saveList(List<TobjBusLine> list) {
        for (TobjBusLine entity : list) {
            tobjBusLineMapper.insertSelective(entity);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void deleteByDate(ParamDto paramDto) {
        tobjBusLineMapper.deleteByDate(paramDto);
    }

    @Override
    @DS("master")
    public List<BusLine> getByDate(String date) {
        Example example = new Example(TobjBusLine.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("rq", date);
        c.andEqualTo("isremove", 0);
        List<TobjBusLine> list = tobjBusLineMapper.selectByExample(example);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<BusLine> busLineList = new ArrayList<>();
        for (TobjBusLine tobjBusLine : list) {
            BusLine busLine = new BusLine();
            busLine.setId(tobjBusLine.getBusline_id());
            busLine.setXldm(tobjBusLine.getLinecode());
            busLine.setXlmc(tobjBusLine.getFullname());
            busLine.setSsqyid(tobjBusLine.getEnterprise_id());
            busLine.setZxlid(tobjBusLine.getMainline_id());
            busLine.setDdfs(tobjBusLine.getDispatch());
            busLine.setXltdlx(tobjBusLine.getDispatch());
            busLine.setXlqs(tobjBusLine.getLinebelong());
            if ("1".equals(tobjBusLine.getStatus())) {
                busLine.setZt("0");
            } else {
                busLine.setZt("1");
            }
            busLine.setCllx(tobjBusLine.getVehicletype());
            busLine.setSfkt(tobjBusLine.getAirconditiontype());
            busLine.setSfwrsp(tobjBusLine.getTickettype());
            busLine.setXmlb(tobjBusLine.getLinenametype());
            busLine.setYysj(tobjBusLine.getOperationtype());
            if (tobjBusLine.getLength() != null) {
                busLine.setXlcd(String.valueOf(tobjBusLine.getLength()));
            }
            busLine.setXzr(tobjBusLine.getAddoperation());
            if (tobjBusLine.getAddtime() != null) {
                busLine.setXzsj(sdf.format(tobjBusLine.getAddtime()));
            }
            busLine.setGxr(tobjBusLine.getUpdateoperation());
            if (tobjBusLine.getUpdatetime() != null) {
                busLine.setGxsj(sdf.format(tobjBusLine.getUpdatetime()));
            }
            if (tobjBusLine.getAuthorizedage() != null) {
                busLine.setSqnx(String.valueOf(tobjBusLine.getAuthorizedage()));
            }
            if (tobjBusLine.getStartdate() != null) {
                busLine.setQsrq(sdf.format(tobjBusLine.getStartdate()));
            }
            if (tobjBusLine.getEnddate() != null) {
                busLine.setZzrq(sdf.format(tobjBusLine.getEnddate()));
            }
            if (tobjBusLine.getActualenddate() != null) {
                busLine.setSjjsrq(sdf.format(tobjBusLine.getActualenddate()));
            }
            busLine.setJsyy(tobjBusLine.getEndreason());
            busLine.setTdlx(tobjBusLine.getDestionationtype());
            String result0 = busDirectionService.getDirectionById(date, busLine.getId(), "0");
            busLine.setXlsxzx(result0);
            String result1 = busDirectionService.getDirectionById(date, busLine.getId(), "1");
            busLine.setXlxxzx(result1);
            busLineList.add(busLine);
        }
        return busLineList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void deleteAll() {
        busLineMapper.deleteAll();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void insert(List<BusLine> list) {
        for (BusLine entity : list) {
            busLineMapper.insertSelective(entity);
        }
    }
}
